package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eUnionRankActivityType;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_ACTIVITY_MEMBER_CONDITION_DATA, desc = "获取商会成员贡献值")
public class GetActivityMemberConditionDataCmd extends AbstractCommand {

	private int activityId;

	private String unionUid;

	private UnionActivityConditionData unionActivityConditionData;

	private ActivityInfo activityInfo;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ActivityProto.ActivityConditionUnionMemberDataReqMsg reqMsg = ActivityProto.ActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		activityInfo = NormalActivityMgr.getActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}

		if(activityInfo.getType() != eActivityType.UnionRankActivity.getValue()){
			ret = GameErrorCode.E_ACTIVITY_NO_UNION_RANK;
			return;
		}
        unionActivityConditionData = UnionActivityMgr.getUnionActivityConditionData(activityId, unionUid);
		if(null == unionActivityConditionData){
		    ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
		    return;
        }
		/*CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.newBuilder();
		msg.setActivityId(activityId);
		msg.setUnionUid(unionUid);
		msg.setServerId(GameServer.getInstance().getServerId());
		msg.setType(0);
		msg.setRankType(1);
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_MEMBER_VALUE, msg);
		GamePlayerMgr.sendChannelPacket(player.getUserId(), pbMsg);*/
	}

	@Override
	public void sendData(GamePlayer player) {
		ActivityProto.ActivityConditionUnionMemberDataRespMsg.Builder respMsg = ActivityProto.ActivityConditionUnionMemberDataRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			if(null != unionActivityConditionData) {
				ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityPb.parseActivityConditionUnionMemberDataListMsg(
						UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, UnionActivityMgr.getUserActivityConditionDataMap(activityId)));
				respMsg.setDataList(dataMsg);
				if (activityInfo.getType() == eActivityType.UnionRankActivity.getValue()
						&& activityInfo.getChildType() == eUnionRankActivityType.unionExpRiseRank.getValue()) {
					calcUnionTradeExpAdd(unionActivityConditionData.getValue().intValue(), respMsg);
				}
			}
		}
		player.sendPacket(Protocol.U_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
	}

	public void calcUnionTradeExpAdd(int unionExpAdd, ActivityProto.ActivityConditionUnionMemberDataRespMsg.Builder respMsg) {
		int unionTradeExpAdd;
		int unionMemberExpAdd = 0;
		ActivityProto.ActivityConditionUnionMemberDataListMsg dataList = respMsg.getDataList();
		for (ActivityProto.ActivityConditionUnionMemberDataTempMsg dataTempMsg : dataList.getDataListList()) {
			unionMemberExpAdd += Integer.parseInt(dataTempMsg.getValue());
		}
		unionTradeExpAdd = unionExpAdd - unionMemberExpAdd;
		if (unionTradeExpAdd < 0) {
			unionTradeExpAdd = 0;
		}
		respMsg.setParam(unionTradeExpAdd);
	}
}
